package engine.newUi
{
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.data.Scene;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.GameDefine;
	import engine.flag.PlayerActionFlag;
	import engine.flag.ResourcesPath;
	import engine.logic.DieHelpLogic;
	import engine.logic.event.LogicEvent;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.util.FilterUtil;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;
	
	public class PartnerRequestLogic
	{
		/**
		 *MAX_LEN_PER_REC	每单元最大三个格子 
		 * MAX_LEN_PER_REC	每页最小格子数
		 */		
		private const MAX_LEN_PER_REC:int=3,
			MIN_PER_PAGE:int=4;
		private static var _self:PartnerRequestLogic;
		/**
		 * 		PART_NAME	伙伴名字   
		 *		PART_GANG	伙伴门派
		 *		PART_TASK	主线任务需求
		 *		PART_MONEY	货币需求
		 * 		PART_NEED_LEVEL 伙伴邀请等级(也是声望等级)
		 * 		PART_HEAD	伙伴头像
		 * 		PART_COMMEND	是否推荐（1,0）
		 * 		PART_HALF	半身像
		 * 		PART_CONFIG_ID	id
		 * 		PART_SKILL:String	技能
		 *		PART_SKILL_DES:		技能描述
		 *		PART_HP:String		生命
		 *		PART_MAXHP:String	,最大生命
		 *		PART_ING:String		内功
		 *		PART_STA:String		体质
		 *		PART_STR:String		外功
		 * 		PART_PEER:String	声望
		 * 		PART_LEVEL:String		等级
		 */
		private const PART_NAME:String		=	"nname",
			PART_GANG:String	=	"ProfType",
			PART_MONEY:String	=	"NeedGoldCoin",
			PART_HEAD:String	=	"Model",
			PART_COMMEND:String	=	"NeedRecommend",
			PART_HALF:String	=	"Model",
			PART_SKILL:String	=	"DefaultUltraSkill",
			PART_SKILL_DES:String	=	"skill_dest",
			PART_HP:String		=	"HP",
			PART_MAXHP:String	=	"MaxHP",
			PART_ING:String		=	"Ing",
			PART_STA:String		=	"Sta",
			PART_STR:String		=	"Str",
			PART_PEER:String	=	"NeedRepute",
			PART_LEVEL:String	=	ActorPropertyUI.LEVEL;
		public static const 		PART_NEED_LEVEL:String	=	"PartnerInviteLevel",
			PART_TASK:String	=	"NeedTaskID",
			PART_CONFIG_ID:String	=	"ConfigID";
		
		private const MC_DATA:String	="mc_data";
		private const MC_PIC:String	="mc_pic";
		
		private const BG:String	=	"p_mc_";
		private const NAME:String	=	"titleTxt";
		private const ICON:String	=	"lvIcon";
		private const LVL:String	=	"infoTxt";
		private const BTN:String	=	"bt_mc";
		private const BTN_LAB:String=	"btntxt";
		
		/**
		 * 邀请
		 *	归队
		 * 字符串 
		 */		
		private const APPLY_STR:String	=	"UIString_1000574",
			COME_BACK_STR:String="UIString_1000024";
		public static function getInstance():PartnerRequestLogic
		{
			if(_self==null)
			{
				_self=new PartnerRequestLogic();
			}
			return _self;
		}
		private var _ui:PartnerRequestUI;
		public function get ui():PartnerRequestUI 
		{
			if(!_ui)
				_ui=new PartnerRequestUI;
			return _ui;
		}
		public function PartnerRequestLogic()
		{
		}
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _scene:Scene;
		
		private var _bindFlag:int	=	-1;
		private var _bindAwayFlag:int	=	-1;
		private var _bindPeerFlag:int=-1;
		private var _bindMoneyFlag:int=	-1;
		/**
		 *储存所有伙伴 
		 */		
		private var _partnerALL:Object;
		/**
		 */		
		private var _inviteAll:Object;
		
		private var _retireAll:Object
		
		private var _bindInitFlag:Boolean=false;
		private var _hasPartnerInvite:Boolean=false;
		
		/**
		 *当前选择招募的ID 
		 */		
		private var _currSelectConfigID:int;
		/**
		 *可以招募的最大位置 
		 */		
		private var _maxGroup:Object=new Object();
		/**
		 *储存当前所需要显示的level 
		 */		
		private var _partnerLevelShow:Array=[];
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_scene=_model.scene;
		}
		private function initAccept():void
		{
			var _currentAcceptTable	:Object=PartnerGroup.CurrentAcceptTable;
			var _currentAcceptPartner:Object=PartnerGroup.CurrentAcceptPartner;
			if(_currentAcceptTable)return;
			_currentAcceptTable=StaticIni.getIniObj("PartnerInvite_SPecial");
			for (var i:String in _currentAcceptTable) 
			{
				_currentAcceptPartner[_currentAcceptTable[i]]=i;
			}
			
		}
		public function	BeforeShow():void
		{
			isSort=false;
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					updataALl();
					_model.removeEventListener(LogicEvent.ITEMICONLOADED,loaded)
				}
			}
			_bindInitFlag=true;
			if(!_partnerALL)
			{
				_partnerALL={};
				ransackConfig();
				initAccept();
				_ui.mainGroup.x=0;
				_ui.mainGroup.y=0;
				_ui.scrolPanel.addView(_ui.mainGroup);
				_ui.actorClip.buttonMode=true;
				_ui.actorClip.addLoadedEvent(function(val:MovieClip):void{actorClick(null)},Event.COMPLETE)
			}
			else
			{
				
			}
			resetReputeLevel()
			ransackPartner();
			ransackRetire();
			updataALl();
			addAllEventListener();
			//			var len:int	=	_ui.mainGroup.row	=	Math.max(_partnerLevelShow.length,MIN_PER_PAGE);
			//			var scorllPos:Number= int(_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE))/len;
			//			_ui.scrolPanel.hscroll=scorllPos;
			_ui.scrolPanel.vscroll=0;
		}
		/**
		 *对比声望等级 
		 * 
		 */		
		private function resetReputeLevel():void
		{
			var reputeLevel:int=_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE);
//			trace("PartnerRequestLogic.resetReputeLevel()当前自己的声望等级：",reputeLevel);
			for  (var i:String in _partnerALL) 
			{
				if(_partnerALL[i].useAble)
				{
					_partnerLevelShow.push(int(i));
				}
			}
		}
		private var isSort:Boolean=false;
		/**
		 *按照降序排序 
		 * 
		 */		
		private function sortShowLevelArr():void
		{
			_partnerLevelShow.sort(Array.NUMERIC);
			if(_partnerLevelShow.length<MIN_PER_PAGE)
			{
				var len:int=_partnerLevelShow.length;
				for (var i:int = 0; i <MIN_PER_PAGE-len; i++) 
				{
					_partnerLevelShow.push(-1);
				}
				
			}
			if(!isSort)
			{
				_partnerLevelShow.reverse();
			}
			isSort=true;
		}
		/**
		 *数据准备完毕 后 刷新显示 
		 * 
		 */		
		private function updataALl():void
		{
			_hasPartnerInvite = false;
			//遍历声望
			var len:int	=	_ui.mainGroup.row	=	Math.max(_partnerLevelShow.length,MIN_PER_PAGE);
			_ui.mainGroup.x=0;
			_ui.mainGroup.y=0;
			_ui.scrolPanel.addView(_ui.mainGroup);
			//			var scorllPos:Number= int(_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE))/len;
			//			_ui.scrolPanel.hscroll=scorllPos;
			resetMainGroup();
			sortShowLevelArr();
			for (var i:int = 0; i < len; i++) 
			{
				var mc:MovieClip=_ui.mainGroup.getItemByIndex(i);
				var level:int=_partnerLevelShow[i];
				if(level>=0)
				{
					updateSingle(_partnerALL[level],mc);
				}
				else
				{
					showDisable(mc,MIN_PER_PAGE-i);
				}
			}
		}
		private function showDisable(mc:MovieClip,level:int):void
		{
			for (var i:int = 0; i < MAX_LEN_PER_REC	; i++) 
			{
				var m:MovieClip=getHeadByIndex(mc,i);
				m.visible=false;
			}
			mc[ICON].visible=false;
			trace("PartnerRequestLogic.showDisable(mc, level)",level);
			mc[ICON].gotoAndStop(level);
			mc["txt"].visible=true;
			var strID:String=check(level)?"UIString_1000572":"UIString_1000571";
			mc["txt"].htmlText=StaticIni.getSwfLangStr(strID);
			
			
		}
		private function updataByGroup(_data:PartnerGroup):void
		{
			var level:int=_data.Level;
			var currentMc:MovieClip=_ui.mainGroup.getItemByIndex(_partnerLevelShow.indexOf(level));
			updateSingle(_data,currentMc);
		}
		/**
		 *锁tips 
		 * @param e
		 * 
		 */		
		private function rollOver(e:MouseEvent):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			var icon:MovieClip=mc[ICON] as MovieClip;
			if(mc["lock"].contains(e.target as DisplayObject))
			{
				var level:int=icon.currentFrame;
				_tip.showMain(StaticIni.getSwfLangStrVar("UIString_1000570",[reputeLevelEx(level),StaticIni.getIniVar("PartnerGradeNeedLevel","PartnerGradeNeedLevel_"+level)]));
			}
			else
			{
				_tip.hide();
				var m:MovieClip=getMouseOverHeadMc(mc,e);
				rollHead=m;
			}
		}
		private function reputeLevelEx(currentLevel:int):int
		{
			var obj:Object=StaticIni.getIniObj("PartnerInviteLevel");
			var exp:int=obj[currentLevel];
			return exp;//-_role.GetPropertiesEx(ActorPropertyUI.REPUTATION);
		}
		private function rollOut(e:Event):void
		{
			_tip.hide();
			rollHead=null;
		}
		private var _rollHead:MovieClip;
		public function set rollHead(value:MovieClip):void 
		{
			if(_rollHead)
			{
				_rollHead.gotoAndStop(1)
			}
			_rollHead	=	value;
			if(_rollHead)
			{
				_rollHead.gotoAndStop(2)
			}
		}
		/**
		 *检查是招募完 还是未满足 
		 * @param lv
		 * @return 
		 * 
		 */		
		private function check(lv:int):Boolean
		{
			
			var partnerVo:View		=	_model.scene.GetView(View.VIEWPORT_PARTNER);
			if(_partnerALL[lv]==null)return true;
			for each(var i:* in _partnerALL[lv].partnerVecor)
			{
				return checkSelf(PartnerGroup.getPropertyIn(i,PART_CONFIG_ID));  //已招募过   全部招募
			}
			return false;   //为满足
			function checkSelf(congfigid:int):Boolean
			{
				if(partnerVo !=null)
				{
					for (var property:String in partnerVo.Properties) 
					{
						if(int(property))
						{
							if(partnerVo.CheckProperties(property))
							{
								var item:ViewObject=partnerVo.GetPropertiesEx(property);
								if(item.GetPropertiesEx("ConfigID")==congfigid)
								{
									return true;
								}
							}
							
						}
					}
				}
				
				return false;
			}
		}
		/**
		 *更新单个 
		 * @param data
		 * @param mc
		 * 
		 */		
		private function updateSingle(_data:PartnerGroup,mc:MovieClip):void
		{
			if(!_data)return;
			if(!mc)return ;
			var showArr:Array=_data.getShowPartners();
			var len:int=showArr.length;
			mc[MC_DATA]=showArr;
			for (var i:int = 0; i < MAX_LEN_PER_REC	; i++) 
			{
				var m:MovieClip=getHeadByIndex(mc,i);
				
				if(requireLevel()	&&	i<len)
				{
					if(m["comand"])
					{
						(m["comand"] as NxClip).source = PartnerGroup.getPropertyIn(d,PART_COMMEND)?"":"";
					}
					var d:Object=showArr[i];
					m.visible=true;
					m.buttonMode=true;
					m.gotoAndStop(1);
					pic(PartnerGroup.getPropertyIn(d,PART_HEAD),PartnerGroup.getPropertyIn(d,PART_COMMEND),PartnerGroup.getPropertyIn(d,PART_NEED_LEVEL));
					name(PartnerGroup.getPropertyIn(d,PART_CONFIG_ID));
					level(PartnerGroup.getPropertyIn(d,PART_LEVEL));
					btnLab(d is ViewObject?COME_BACK_STR:APPLY_STR);
					btnEnable(d);
					var needGoldCoin:int = PartnerGroup.getPropertyIn(d,PART_MONEY);
					var goldcoin:int = _model.scene.Role.getGoldCoin();
					var needRepute:int = PartnerGroup.getPropertyIn(d,PART_PEER);
					var repute:int = _model.scene.Role.GetProperties("NeedRepute");
					if( needGoldCoin <= goldcoin && needRepute <= repute)
						_hasPartnerInvite=true;
					//					S
				}
				else
				{
					m.visible=false;
				}
			}
			mc["txt"].visible	=	!len;
			
			var strID:String;
			var selfLv:int=int(_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE));
			if(_data.Level>selfLv)  //设置高于自己的
			{
				strID="UIString_1000571"  //尚未到达
			}
			else   //低于自己的
			{
				
				var partnerVo:View		=	_model.scene.GetView(View.VIEWPORT_PARTNER);
				strID=check(_data.Level)?"UIString_1000572":"UIString_1000571"
				
			}
			mc["txt"].htmlText	=	StaticIni.getSwfLangStr(strID);
			
			//帧数与等级的过滤
			function requireLevel():Boolean
			{
				var b:Boolean=_data.Level <=	mc[ICON].totalFrames;
				mc[ICON].gotoAndStop(_data.Level);
				mc[ICON].visible=_data.checkGroup()==2;
				return b;
			}
			function pic(configid:int,part_comment:int,partnerNeedLevel:int):void
			{
				if(!m[MC_PIC])
				{
					m[MC_PIC]=new NxClip;
					m["img2"].addChild(m[MC_PIC]);
					if(part_comment == 1 && partnerNeedLevel>=int(_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE)))
					{
						var commend_Pic : NxClip = new NxClip();
						commend_Pic.x = -5;
						commend_Pic.y = -5;
						commend_Pic.source = ResManager.newResClass("CustomControl_0119");
						m["img2"].addChild(commend_Pic)
					}
				}
				m[MC_PIC].source=ResourcesPath.getMiniHeadPic(configid);
			}
			function name(configid:String):void
			{
				m[NAME].htmlText=StaticIni.getSwfLangStrVar("PlayerProperty_20",[StaticIni.getSwfLangStr(configid)]);
			}
			function level(str:String):void
			{
				m[LVL].htmlText=StaticIni.getSwfLangStrVar("PlayerProperty_19",[str]);
			}
			function btnLab(str:String):void
			{
				(m[BTN_LAB] as TextField).mouseEnabled=false;
				m[BTN_LAB].htmlText=StaticIni.getSwfLangStr(str);
			}
			function btnEnable():void
			{
				var errStr:String=applyCondition(d);
				if(!errStr)
				{
					(m[BTN] as SimpleButton).enabled=true;
					m[BTN].filters=null;
					m[BTN_LAB].filters=null;
				}
				else
				{
					m[BTN].enabled=false;
					m[BTN].filters=[FilterUtil.GrayColorFilter]
					m[BTN_LAB].filters=[FilterUtil.GrayColorFilter]
				}
			}
			/**
			 *能否申请 
			 * @return 
			 * 
			 */		
			function applyCondition(arr:Object):String
			{
				if(arr is ViewObject)return "";
				var moneyNeed:int	=	arr[PART_MONEY]
				if(arr[PART_PEER]	> _role.GetPropertiesEx("Repute"))
					return PART_PEER;
				if(moneyNeed	>	_role.getGoldCoin())
					return PART_MONEY;
				if(arr[PART_NEED_LEVEL]>_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE))
					return PART_NEED_LEVEL;
				return "";
			}
		}
		
		private function getHeadByIndex(mc:MovieClip,index:int):MovieClip
		{
			return mc[BG+index];
		}
		/**
		 **根据等级获得改等级的可邀请伙伴 
		 * @param level
		 * @return 
		 * 
		 */
		public function getPartnerByLevel(level : int):Object
		{
			if(_partnerALL == null)
			{
				_partnerALL={};
				ransackConfig();
			}
			if(level>10)
			{
				return null;
			}
			var obj : Object;
			obj = _partnerALL[level];
			return obj;
		}
		
		/**
		 *遍历 伙伴配置 (伙伴)
		 *  //这个只需要初始化 遍历一次
		 * 
		 */		
		private function ransackConfig():void
		{
			//资源域名前缀
			const PARTNER:String="21001001"
			var tArr:Object;
			var i:int=int(PARTNER);
			do
			{
				tArr=getPartnerInfo(i);
				i++;
				//				trace("PartnerInviteLogic.ransackPartner()",i);
				setPartner(tArr);
			}while(tArr!=null)
			//			refreshIndex();
		}
		/**
		 *遍历玩家伙伴 
		 * 
		 */		
		private function ransackPartner():void
		{
			var view:View=_model.scene.GetView(View.VIEWPORT_PARTNER);
			view.getDataForEach(function (key:Object,value:Object):void
			{
				var viewobj:ViewObject=value as ViewObject
				if(viewobj)
				{
					var level:int=PartnerGroup.getLevel(viewobj);
					_partnerALL[level].addViewPartner(viewobj);
				}
			}
			)
			//				func.call(this,p,GetProperties(p));
		}
		/**
		 * 遍历离队伙伴 
		 * 
		 */		
		private function ransackRetire():void
		{
			var view:View=_model.scene.GetView(View.VIEWPORT_RETIRE_PARTNER);
			view.getDataForEach(function (key:Object,value:Object):void
			{
				var viewobj:ViewObject=value as ViewObject
				if(viewobj)
				{
					var level:int=PartnerGroup.getLevel(viewobj);
					_partnerALL[level].addRetirePartner(viewobj);
				}
			}
			)
		}
		/**
		 *读取伙伴本地配置 
		 * @param id
		 * @return 
		 * 
		 */		
		private function getPartnerInfo(id:int):Object
		{
			var obj:*=_tip.getStaticObjWithID(id+"")//StaticIni.getIniObj(index+"");
			//StaticIni.getSwfLangStr(PARTNER+index);
			if(obj)
			{
				obj[PART_CONFIG_ID]	=	id;
				obj[PART_MAXHP]=GameDefine.GET_MAXHP(obj[PART_STA],obj[PART_LEVEL]);
			}
			return obj;
		}
		/**
		 * 写文件设置
		 * @param arr			 
		 * @see getPartnerInfo
		 */		
		private function setPartner(arr:Object):void
		{
			if(arr)
			{
				var level:int=arr[PART_NEED_LEVEL];
				var group:PartnerGroup=_partnerALL[level];
				if(!group)
				{
					_partnerALL[level]=group=new PartnerGroup(level);
				}
				group.addConfigPartner(arr);
			}
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
			_partnerLevelShow=[];
			_bindInitFlag=false;
			for each (var i:PartnerGroup in _partnerALL) 
			{
				i.resetServerdata();
			}
		}
		private  function getMouseOverHeadMc(mc:MovieClip,e:MouseEvent):MovieClip
		{
			for (var i:int = 0; i < MAX_LEN_PER_REC; i++) 
			{
				var m:MovieClip=getHeadByIndex(mc,i);
				if(m.contains(e.target as DisplayObject))
					return m
			}
			return null;
		}
		/**
		 *group单击 
		 * 
		 */		
		private function clickHandle(e:MouseEvent):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			var m:MovieClip=getMouseOverHeadMc(mc,e);
			if(!m)return ;
			var index:int=int(m.name.charAt(m.name.length-1));
			var data:Object=mc[MC_DATA][index];
			rightData=data;
			if(e.target is SimpleButton		&&	e.target.enabled)
			{
				applyRequestPartner(PartnerGroup.getPropertyIn(data,PART_CONFIG_ID));
			}
			//			actorClick(e);
		}
		/**
		 *设置右边信息 
		 * @param data
		 * 
		 */		
		private function set rightData(data:Object):void
		{
			if(!data)return ;
			var	plist:Array=[];
			ui.nameLable.htmlText =StaticIni.getSwfLangStrVar("UIString_1000610",[StaticIni.getSwfLangStr(PartnerGroup.getPropertyIn(data,PART_CONFIG_ID))]);
			//			plist.push(StaticIni.getSwfLangStr(PartnerGroup.getPropertyIn(data,PART_CONFIG_ID)));
			var mxHp:String=DetailPropertyLogic.getInstance().setData(data).maxlife;
			plist.push(objTranfer(/*obj.hasOwnProperty(PART_HP)?obj[PART_HP]:*/mxHp));
			plist.push(objTranfer(PartnerGroup.getPropertyIn(data,PART_STR)));
			plist.push(objTranfer(PartnerGroup.getPropertyIn(data,PART_ING)));
			plist.push(objTranfer(PartnerGroup.getPropertyIn(data,PART_STA)));
			plist.push(GameDefine.PART_PROFTYPE(objTranfer(PartnerGroup.getPropertyIn(data,PART_GANG))));
			var  s:String=StaticIni.getSwfLangStr(objTranfer(PartnerGroup.getPropertyIn(data,PART_SKILL)));
			plist.push(s)
			plist.push((GameDefine.GET_SKILL_DES(int(objTranfer(PartnerGroup.getPropertyIn(data,PART_SKILL))))));
			
			
			s=StaticIni.getSwfLangStrVar("UIString_1000443",plist);
			plist=[];
			plist.push(objTranfer(getColor(PartnerGroup.getPropertyIn(data,PART_MONEY),_role.getGoldCoin())));
			plist.push(objTranfer(getColor(PartnerGroup.getPropertyIn(data,PART_PEER),_role.GetPropertiesEx("Repute"))));
			plist.push(objTranfer(getColor(PartnerGroup.getPropertyIn(data,PART_NEED_LEVEL),_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE))))
			
			
			propertyText=s+"\n"+"\n"+StaticIni.getSwfLangStrVar("stringinvite_001",plist);
			
			
			
			actoraction=data;
			
			storyStr=StaticIni.getSwfLangStr("Des_"+PartnerGroup.getPropertyIn(data,PART_CONFIG_ID));
			
			function objTranfer(obj:*):String
			{
				if(!obj)
					return 0+""
				return obj;
			}
			function getColor(needcount:int,nowCount:int):String
			{
				return _tip.getColorHtml(needcount+"",needcount>nowCount?"ff0000":"")
			}
		}
		/**
		 *点击当前人物模型 
		 * 
		 */		
		private function actorClick(e:Event):void
		{
			if((_ui.actorClip.content as MovieClip)&&((_ui.actorClip.content as MovieClip).getChildAt( 0 ) as MovieClip))
			{
				((_ui.actorClip.content as MovieClip).getChildAt( 0 ) as MovieClip).gotoAndPlay(PlayerActionFlag.Magic);
			}
		}
		private function resetMainGroup():void
		{
			_ui.mainGroup.mouseEnabled=false;
		}
		public function addAllEventListener():void
		{
			_ui.mainGroup.addItemsEvent(MouseEvent.CLICK,clickHandle);
			_ui.mainGroup.addItemsEvent(MouseEvent.MOUSE_OVER,rollOver);
			_ui.mainGroup.addItemsEvent(MouseEvent.MOUSE_OUT,rollOut);
			_ui.actorClip.addEventListener(MouseEvent.CLICK,actorClick);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			if( _bindFlag == -1 )
			{
				//				trace("PartnerInviteLogic.addAllListener()","绑定视图伙伴");
				_bindFlag		=	_bind.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
			}
			if(_bindMoneyFlag==-1)
			{
				_bindMoneyFlag		=	_bind.BindProp(_role,"GoldCoin",this,"money");				
			}
			if(_bindPeerFlag==-1)
			{
				_bindPeerFlag		=	_bind.BindProp(_role,"Repute",this,"peerCount");				
			}
		}
		public function removeAllEventListener():void
		{
			_ui.mainGroup.removeItemsEvent(MouseEvent.CLICK,clickHandle);
			_ui.mainGroup.removeItemsEvent(MouseEvent.ROLL_OVER,rollOver);
			_ui.mainGroup.removeItemsEvent(MouseEvent.ROLL_OUT,rollOut);
			if( _bindFlag != -1 )
			{
				_bindFlag		=	_bind.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
			}
			if(_bindMoneyFlag!=-1)
			{
				_bindMoneyFlag		=	_bind.UnbindProp(_role,"GoldCoin",_bindMoneyFlag);				
			}
			if(_bindPeerFlag!=-1)
			{
				_bindPeerFlag		=	_bind.UnbindProp(_role,"Repute",_bindPeerFlag);				
			}
		}
		public function set money(value:int):void
		{
			ui.Text2.htmlText=StaticIni.getSwfLangStrVar("UIString_1000026",[value]);
		}
		public function set peerCount(value:int):void 
		{
			ui.Text1.htmlText=StaticIni.getSwfLangStrVar("UIString_1000573",[value])
		}
		/**
		 * 侠客传记
		 * @param str
		 * 
		 */		
		private function set storyStr(str:String):void
		{
			_ui.storyText.htmlText=str;
		}
		/**
		 *属性 
		 * @param str
		 * 
		 */		
		private function set propertyText(str:String):void
		{
			_ui.propertyText.htmlText=str;
		}
		/**
		 *说明信息 
		 * @param str
		 * 
		 */		
		private function set attentionText(str:String):void
		{
			_ui.attentionText.htmlText=str;
		}
		/**
		 *人物模型 
		 * @param obj
		 * 
		 */		
		private function set actoraction(obj:Object):void
		{
			var data:ObjectData=obj as ObjectData;
			if(data)
			{
				_ui.actorClip.source =ResourcesPath.getModel(data);
			}
			else
			{
				_ui.actorClip.source =	ResPath.ACTORS+"npc_"+obj[ActorPropertyUI.MODEL]+".swf"
			}
		}
		public function get hasPartnerInvite():Boolean
		{
			if(_bindInitFlag)
				return _hasPartnerInvite;
			BeforeShow()
			AfterHide()
			return _hasPartnerInvite;
		}
		public function get checkGuide():int
		{
			if(hasPartnerInvite)//如果有未招募伙伴，提示招募伙伴；      
			{
				return DieHelpLogic.BUDDY;
			}
			if(MyPropertyLogic.getInstance().isPartnerNotEmbattle)//	如果招募了伙伴但没有布阵，提示布阵；
			{
				return DieHelpLogic.EMBATTLE;
			}
			return -1;
		}
		/**
		 *邀请伙伴 
		 * @param configid
		 * 
		 */		
		private function applyRequestPartner(configid:int):void
		{
			_currSelectConfigID = configid;
			trace("PartnerRequestLogic.applyRequestPartner(configid)请求伙伴");
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_INVITE_PARTNER,int(configid));
		}
		/**
		 *添加伙伴  、、意味着 要从显示中删除掉 
		 * @param PropertiesName
		 * @param PropertiesValue
		 * @param data
		 * 
		 */		
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			var level:int=PartnerGroup.getLevel(PropertiesValue);
			var group:PartnerGroup=(_partnerALL[level]);
			group.addViewPartner(PropertiesValue);
			updataByGroup(group);
			if(_currSelectConfigID == int(PropertiesValue.GetProperties("ConfigID"))) //招募后删除 右边
			{
				ui.nameLable.htmlText ="";
				propertyText="";
				storyStr="";
				_ui.actorClip.source=null;
			}
		}
		//不做处理
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
	}
}
import engine.data.ModelLocator;
import engine.data.RoleVo;
import engine.data.Scene;
import engine.data.ViewObject;
import engine.newUi.ActorPropertyUI;
import engine.newUi.PartnerRequestLogic;

import newx.BaseObject;
import newx.com.config.StaticIni;

/**
 *同一品级的伙伴群组 
 * @author yanghr
 * 
 */
class PartnerGroup
{
	/**
	 *特殊处理  某些伙伴在接任务之后出现   无视其他条件 
	 */		
	public static  var CurrentAcceptTable:Object,CurrentAcceptPartner:Object=new Object;
	
	public static function getLevel(viewobj:ViewObject):int
	{
		if(viewobj.CheckProperties(LEVEL))
			return viewobj.GetProperties(LEVEL);
		return StaticIni.getIniObj(viewobj.GetPropertiesEx(CONFIGID))[LEVEL];
	}
	public static function getPropertyIn(partner:Object,propertyname:String):*
	{
		if(partner is BaseObject)
		{
			return (partner as BaseObject).GetProperties(propertyname)
		}
		return partner[propertyname];
	}
	/** 
	 *品级属性名 
	 */	
	private  static  const LEVEL:String=PartnerRequestLogic.PART_NEED_LEVEL;
	private const TASK:String=PartnerRequestLogic.PART_TASK
	private static const CONFIGID:String=PartnerRequestLogic.PART_CONFIG_ID;
	
	private var _level:int
	/**
	 *品级 
	 */	
	public function get Level():int
	{
		return _level
	}
	public function PartnerGroup(level:int)
	{
		_level=level
	}
	/**
	 *配置表数据 
	 */	
	private var partnerVec:Array=[];
	/**
	 *上阵伙伴数据 
	 */	
	private var partnerViewObj:Object={};
	/**
	 *归队伙伴数据 
	 */	
	private var partnerExObj:Object={};
	
	public function get partnerVecor():Array
	{
		return partnerVec;
	}
	/**
	 *得到显示数组 
	 * @return 
	 * 
	 */	
	public function getShowPartners():Array
	{
		var arr:Array=[];
		var checkType:int=checkGroup();
		if(checkType==1)
		{
			var showArr:Array=[];
			//把可以显示的对象 再检查任务是否完成  能就显示
			for each (var ii:Object in partnerVec) 
			{
				if(checkisAccepted(ii))
				{
					showArr.push(ii);
				}
			}
			return showArr//partnerVec.concat();
		}
		else
			for each (var i:Object in partnerVec) 
		{
			var id:int=getPropertyIn(i,CONFIGID);
			if(partnerViewObj.hasOwnProperty(id))
			{
				continue;//	return [];
			}
			if(partnerExObj.hasOwnProperty(id))
			{
				arr.push(partnerExObj[id]);
				continue;
			}
			if(checkTask(i))
			{
				arr.push(i);
			}
		}
		//是否需要排序？？
		return arr;
	}
	/**
	 * 往组里添加  
	 * @param partner
	 * 
	 */	
	public function addConfigPartner(partner:Object):void
	{
		if(checkLevel(partner))
		{
			if(partnerVec.indexOf(partner)==-1)
			{
				partnerVec.unshift(partner)
			}
			
		}
	}
	/**
	 * 
	 * @param partner
	 * 
	 */	
	public function addViewPartner(partner:ViewObject):void
	{
		if(checkLevel(partner))
		{
			partnerViewObj[getPropertyIn(partner,CONFIGID)]	=	partner;
		}
	}
	public function addRetirePartner(partner:ViewObject):void
	{
		if(checkLevel(partner))
		{
			partnerExObj[getPropertyIn(partner,CONFIGID)]	=	partner;
		}
	}
	/**
	 *检查是否符合显示该群组的条件 
	 * @return 
	 * 
	 */	
	public function checkGroup():int
	{
		var _role:RoleVo=ModelLocator.getInstance().scene.Role;
		//伙伴等级需求
		var lvl:int=_role.GetPropertiesEx(ActorPropertyUI.PARTNER_INVITE);
		var needLv:int = int(StaticIni.getIniVar("PartnerGradeNeedLevel","PartnerGradeNeedLevel_"+Level));
		var roleLv:int = _role.getPlayerLevel();
		if(Level>lvl+1)
		{
			return 0;//完全不显示
		}
		else if(roleLv<needLv)    //
		{
			return 0;//完全不显示
		}
		else if(Level==lvl+1)
		{
			return 1//显示头像但不能邀请
		}
		return 2;//可以邀请
	}
	/**
	 * 
	 //主线任务需求
	 * @param partner
	 * @return 
	 * 
	 */	
	public function checkTask(partner:Object):Boolean
	{
		var b:Boolean;
		var _scene:Scene=ModelLocator.getInstance().scene;
		if(getPropertyIn(partner,TASK)	&&	getPropertyIn(partner,TASK)!="0")
		{
			var arr2:Array=_scene.GetRecordEx("Task_Completed").recArr
			for each (var ia:Array in arr2) 
			{
				if(ia[0]==int(getPropertyIn(partner,TASK)))
				{
					return true
				}
			}
			return false;
		}
		return true;
	}
	/**
	 *检查是否属于接受任务显示条件 
	 * @param partner
	 * @return 
	 * 
	 */	
	public function checkneedAccept(partner:Object):Boolean
	{
		return (CurrentAcceptPartner.hasOwnProperty(getPropertyIn(partner,CONFIGID)))
	}
	/**
	 *检查接受任务条件满足 
	 * @param partner
	 * @return 
	 * 
	 */	
	public function checkisAccept(partner:Object):Boolean
	{
		var _scene:Scene=ModelLocator.getInstance().scene;
		var id:String=CurrentAcceptPartner[getPropertyIn(partner,CONFIGID)]
		if(id	&&	id!="0")
		{
			var arr2:Array=_scene.GetRecordEx("Task_Accepted").recArr
			for each (var ia:Array in arr2) 
			{
				if(ia[0]==int(id))
				{
					return true
				}
			}
		}
		return false;
	}
	/**
	 *检查任务完成条件满足   待定
	 * @param partner
	 * @return 
	 * 
	 */	
	public function checkisAccepted(partner:Object):Boolean
	{
		var _scene:Scene=ModelLocator.getInstance().scene;
		var id:String=getPropertyIn(partner,TASK);   //检查需要完成的任务
		if(id=="0")  //不需要任务
		{
			return true;
		}
		else if(id	&&	id!="0")
		{
			var arr2:Array=_scene.GetRecordEx("Task_Completed").recArr       
			for each (var ia:Array in arr2) 
			{
				if(ia[0]==int(id))
				{
					return true
				}
			}
		}
		return false;
	}
	/**
	 *检查伙伴品级是不是群组所对应的品级 
	 * @param partner
	 * @return 
	 * 
	 */	
	public function checkLevel(partner:Object):Boolean
	{
		if(partner is ViewObject)
			return getLevel(partner as ViewObject)==Level
		return partner[LEVEL]==Level;
	}
	/**
	 *是否可以邀请这一品级 
	 */	
	public function get useAble():Boolean 
	{ 
		return Boolean(checkGroup()); 
	}
	/**
	 *重置服务器数据 
	 * 
	 */	
	public function resetServerdata():void
	{
		partnerViewObj	=	{};
		partnerExObj	=	{}//new Vector.<ViewObject>;
	}
}